package com.bugbuff.dubbo.server.api;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.bugbuff.dubbo.server.enums.CommentType;
import com.bugbuff.dubbo.server.pojo.Comment;
import com.bugbuff.dubbo.server.pojo.Video;
import com.bugbuff.dubbo.server.vo.PageInfo;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service

public class QueryInfoApiImpl implements  QueryInfoListApi {

    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public List<Video> queryVideoList(Integer pagesize, Integer page, String sortProp, String sortOrder, Long uid) {
        //sortProp 排序字段
        // sortOrder ascending 升序 descending 降序



      //  PageRequest pageRequest = PageRequest.of(page- 1, pagesize, Sort.by(Sort.Order.desc("created")));
        Query query = new Query(Criteria.where("userId").is(uid));
        List<Video> videoList = this.mongoTemplate.find(query, Video.class);


        return videoList;

    }

    @Override
    public Long queryLikeCount(String uid) {
        Video video = this.mongoTemplate.findById(uid, Video.class);
        return null;
    }

    /**
     * 查询评论列表
     * @param pagesize
     * @param page
     * @param
     * @return
     */
    @Override
    public PageInfo<Comment> queryCommentList(Integer pagesize, Integer page, String publishId) {

        PageInfo<Comment> commentPageInfo = this.queryCommentList(publishId, page, pagesize);
        return commentPageInfo;
    }


    /**
     * 查询评论列表
     *
     * @param publishId
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<Comment> queryCommentList(String publishId, Integer page, Integer pageSize) {
        PageRequest pageRequest = PageRequest.of(page - 1, pageSize, Sort.by(Sort.Order.asc("created")));

        /*Query query = new Query(Criteria
                .where("publishId").is(new ObjectId(publishId))
                .and("commentType").is(CommentType.COMMENT.getType())).with(pageRequest);*/

        //查询评论列表
        Query query=Query.query(Criteria.where("publishId").is(new ObjectId(publishId)).and("commentType").is(CommentType.COMMENT.getType())).with(pageRequest);
        List<Comment> commentList = this.mongoTemplate.find(query, Comment.class);

        PageInfo<Comment> pageInfo = new PageInfo<>();
        pageInfo.setPageNum(page);
        pageInfo.setPageSize(pageSize);
        pageInfo.setRecords(commentList);
        return pageInfo;
    }



    @Override
    public Integer userVideoCount(Long uid) {
        Query query = new Query(Criteria.where("userId").is(uid));
        List<Video> videoList = this.mongoTemplate.find(query, Video.class);


        return videoList.size();
    }

    @Override
    public Integer userCommentCount(String publishId) {
        Query query = new Query(Criteria.where("publishId").is(new ObjectId(publishId))
        .and("commentType").is(CommentType.COMMENT.getType()));
        List<Comment> commetList = this.mongoTemplate.find(query, Comment.class);
        return  commetList.size();
    }


}
